package com.chaodriod.common.system;

import android.app.Service;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioManager;
import android.os.Build;
import android.provider.Settings;

/**
 * Created by CHEN on 2018-2-28.
 */

public class SystemSettingUtils {

    /**
     * 启动系统触摸提示音
     */
    public static void setTouchEffectSound(Context context, boolean on) {
        AudioManager mAudioManager = (AudioManager) context.getSystemService(Service.AUDIO_SERVICE);
        if (on) {
            Settings.System.putInt(context.getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED, 1);
            mAudioManager.loadSoundEffects();
        } else {
            mAudioManager.unloadSoundEffects();
            Settings.System.putInt(context.getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED, 0);
        }
    }

    /**
     * 使能/禁用 摄像头
     */
    public static void setCameraDisabled(Context context, boolean off) {
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
        ComponentName mAdminName = new ComponentName(context, DeviceAdminReceiver.class);
        // 判断该组件是否有系统管理员的权限
        boolean isAdminActive = devicePolicyManager.isAdminActive(mAdminName);
        devicePolicyManager.setCameraDisabled(mAdminName, off);
    }

    /**
     * 打开未知来源权限
     *
     * @param context
     */
    public static void openNonMarketAppsPerm(Context context) {
        if (Build.VERSION.SDK_INT < 17) {
            int flag = Settings.Secure.getInt(context.getContentResolver(),
                    Settings.Secure.INSTALL_NON_MARKET_APPS, 0);
            if (flag == 0) {
                Settings.Secure.putInt(context.getContentResolver(),
                        Settings.Secure.INSTALL_NON_MARKET_APPS, 1);
            }
        } else {
            int flag = Settings.Global.getInt(context.getContentResolver(),
                    Settings.Global.INSTALL_NON_MARKET_APPS, 0);
            if (flag == 0) {
                Settings.Global.putInt(context.getContentResolver(),
                        Settings.Global.INSTALL_NON_MARKET_APPS, 1);
            }
        }
    }

    /**
     * 关闭未知来源权限
     *
     * @param context
     */
    public static void closeNonMarketAppsPerm(Context context) {
        if (Build.VERSION.SDK_INT < 17) {
            int flag = Settings.Secure.getInt(context.getContentResolver(),
                    Settings.Secure.INSTALL_NON_MARKET_APPS, 1);
            if (flag == 1) {
                Settings.Secure.putInt(context.getContentResolver(),
                        Settings.Secure.INSTALL_NON_MARKET_APPS, 0);
            }
        } else {
            int flag = Settings.Global.getInt(context.getContentResolver(),
                    Settings.Global.INSTALL_NON_MARKET_APPS, 1);
            if (flag == 1) {
                Settings.Global.putInt(context.getContentResolver(),
                        Settings.Global.INSTALL_NON_MARKET_APPS, 0);
            }
        }
    }

    /**
     * 检查状态是否打开
     *
     * @param context
     */
    public static boolean checkNonMarketAppsPermStatus(Context context) {
        boolean unKnowSource = false;
        if (Build.VERSION.SDK_INT < 17) {
            unKnowSource = Settings.Secure.getInt(context.getContentResolver(),
                    Settings.Secure.INSTALL_NON_MARKET_APPS, 0) == 1;
        } else {
            unKnowSource = Settings.Global.getInt(context.getContentResolver(),
                    Settings.Global.INSTALL_NON_MARKET_APPS, 0) == 1;
        }
        return unKnowSource;
    }

    public static String getDeviceToken(ContentResolver contentResolver) {
        return Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID);
    }


}